# import tensorflow as tf
# import numpy as np
# import pandas as pd
# version = tf.__version__
# gpu_ok = tf.test.is_gpu_available()
# print("tf version:",version,"\nif use GPU",gpu_ok)

# def countAndSay(n):
#     if n == 1:
#         return '1'
#     s = countAndSay(n - 1) + '*'
#     res, count = '', 1
#     for i in range(len(s) - 1):
#         if s[i] == s[i + 1]:
#             count += 1
#         else:
#             res += str(count) + str(s[i])
#             count = 1
#     return res

# -*- coding:utf8 -*-
# /usr/bin/env python


# class Node(object):
#     def __init__(self, data, pnext=None):
#         self.data = data
#         self._next = pnext
#
#     def __repr__(self):
#         return str(self.data)
#
#
# class ChainTable(object):
#     def __init__(self):
#         self.head = None
#         self.length = 0
#
#     def isEmpty(self):
#         return (self.length == 0)
#
#     def append(self, dataOrNode):
#         item = None
#         if isinstance(dataOrNode, Node):
#             item = dataOrNode
#         else:
#             item = Node(dataOrNode)
#
#         if not self.head:
#             self.head = item
#             self.length += 1
#
#         else:
#             node = self.head
#             while node._next:
#                 node = node._next
#             node._next = item
#             self.length += 1
#
#     def delete(self, index):
#         if self.isEmpty():
#             print("this chain table is empty.")
#             return
#
#         if index < 0 or index >= self.length:
#             print('error: out of index')
#             return
#
#         if index == 0:
#             self.head = self.head._next
#             self.length -= 1
#             return
#
#         j = 0
#         node = self.head
#         prev = self.head
#         while node._next and j < index:
#             prev = node
#             node = node._next
#             j += 1
#
#         if j == index:
#             prev._next = node._next
#             self.length -= 1
#
#     def insert(self, index, dataOrNode):
#         if self.isEmpty():
#             print("this chain tabale is empty")
#             return
#
#         if index < 0 or index >= self.length:
#             print("error: out of index")
#             return
#
#         item = None
#         if isinstance(dataOrNode, Node):
#             item = dataOrNode
#         else:
#             item = Node(dataOrNode)
#
#         if index == 0:
#             item._next = self.head
#             self.head = item
#             self.length += 1
#             return
#
#         j = 0
#         node = self.head
#         prev = self.head
#         while node._next and j < index:
#             prev = node
#             node = node._next
#             j += 1
#
#         if j == index:
#             item._next = node
#             prev._next = item
#             self.length += 1
#
#     def update(self, index, data):
#         if self.isEmpty() or index < 0 or index >= self.length:
#             print('error: out of index')
#             return
#         j = 0
#         node = self.head
#         while node._next and j < index:
#             node = node._next
#             j += 1
#
#         if j == index:
#             node.data = data
#
#     def getItem(self, index):
#         if self.isEmpty() or index < 0 or index >= self.length:
#             print("error: out of index")
#             return
#         j = 0
#         node = self.head
#         while node._next and j < index:
#             node = node._next
#             j += 1
#
#         return node.data
#
#     def getIndex(self, data):
#         j = 0
#         if self.isEmpty():
#             print("this chain table is empty")
#             return
#         node = self.head
#         while node:
#             if node.data == data:
#                 return j
#             node = node._next
#             j += 1
#
#         if j == self.length:
#             print("%s not found" % str(data))
#             return
#
#     def clear(self):
#         self.head = None
#         self.length = 0
#
#     def __repr__(self):
#         if self.isEmpty():
#             return "empty chain table"
#         node = self.head
#         nlist = ''
#         while node:
#             nlist += str(node.data) + ' '
#             node = node._next
#         return nlist
#
#     def __getitem__(self, ind):
#         if self.isEmpty() or ind < 0 or ind >= self.length:
#             print("error: out of index")
#             return
#         return self.getItem(ind)
#
#     def __setitem__(self, ind, val):
#         if self.isEmpty() or ind < 0 or ind >= self.length:
#             print("error: out of index")
#             return
#         self.update(ind, val)
#
#     def __len__(self):
#         return self.length


# class LNode(object):
#     def __init__(self, data, p=0):
#         self.data = data
#         self.next = p
#
#
# class LinkList(object):
#     def __init__(self):
#         self.head = None
#
#     def initList(self, data):
#         self.head = LNode(data[0])
#         p = self.head
#         for i in data[1:]:
#             node = LNode(i)
#             p.next = node
#             p = p.next
#
#     def isEmpty(self):
#         if self.head.next == 0:
#             print("Empty list!")
#         else:
#             return 0
#
#     def getLength(self):
#         if self.isEmpty():
#             exit(0)
#
#         p = self.head
#         len = 0
#         while p:
#             len += 1
#             p = p.next
#         return len
#
#     def traveList(self):
#         if self.isEmpty():
#             exit(0)
#         print("\rlink list traving result")
#         p = self.head
#         while p:
#             print(p.data)
#             p = p.next
#
#     def insertElem(self, key, index):
#         if self.isEmpty():
#             exit(0)
#         if index < 0 or index > self.getLength() - 1:
#             print("\rKey Error!")
#             exit(0)
#
#         p = self.head
#         i = 0
#         while i <= index:
#             pre = p
#             p = p.next
#             i += 1
#
#         node = LNode(key)
#         pre.next = node
#         node.next = p
#
#     def deleteElem(self, index):
#         if self.isEmpty():
#             exit(0)
#         if index < 0 or index > self.getLength() - 1:
#             print("\rValue Error! Program Exit.")
#             exit(0)
#
#         i = 0
#         p = self.head
#         # 遍历找到索引值为 index 的结点
#         while p.next:
#             pre = p
#             p = p.next
#             i += 1
#             if i == index:
#                 pre.next = p.next
#                 p = None
#                 return 1
#
#         # p的下一个结点为空说明到了最后一个结点, 删除之即可
#         pre.next = None
#
#     def ReverseLisr(self):
#         if self.isEmpty():
#             exit(0)
#         p = self.head
#         cur = self.head.next
#         p.next = None
#         while cur:
#             tmp = cur.next
#             cur.next = p
#             p = cur
#             cur = tmp
#         self.head = p
#
#
# def removeNthFromEnd(head, n: int):
#     p1 = head.val
#     len = 0
#     while p1:
#         len += 1
#         p1 = p1.next
#
#     i = 0
#     p = head.val
#     while p.next:
#         pre = p
#         p = p.next
#         i += 1
#         if i == len - n:
#             pre.next = p.next
#             p = None
#             return 1
#
# print(removeNthFromEnd([1,2,3,4,5],2))


# data = [1, 2, 3, 4, 5]
# l = LinkList()
# l.initList(data)
# # l.traveList()
#
# # l.insertElem(666,3)
# # l.deleteElem(3)
# l.ReverseLisr()
# l.traveList()

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        head0 = ListNode(0)
        head0.next = head

        runner = head0
        walker = head0

        for i in range(n):
            runner = runner.next

        while runner.next:
            walker = walker.next
            runner = runner.next

        node = walker.next
        walker.next = node.next
        node.next = None
        return head0.next


data = [1,2,3,4,5]
p = ListNode(data)
l = Solution()
l.removeNthFromEnd(p,2)